﻿package org.aquiferserver.client.flex 
{
	import org.aquiferserver.client.flex.logging.Log;
	import org.aquiferserver.client.flex.logging.LogFactory;
	import org.aquiferserver.common.AquiferMessage;
	import org.aquiferserver.common.HandshakeConfirmationMessage;
	import org.aquiferserver.common.HandshakeMessage;
	import flash.events.Event;
	/**
	 * ...
	 * @author Seth Albanese
	 * @version $Id: HandshakeMessageHandler.as 60 2009-06-15 08:47:09Z salbanese0080@gmail.com $
	 */
	public class HandshakeMessageHandler implements MessageHandler
	{
		private var hasResp:Boolean = true;
		private var resp:HandshakeConfirmationMessage;
		
		private var logger:Log = LogFactory.getLog( "org.aquiferserver.client.flex.HandshakeMessageHandler" );
		
		public function handleMessage( msg:AquiferMessage, state:GameState ) : void
		{
			logger.debug( "Recieved handshake message" );
				
			var hsm:HandshakeMessage = HandshakeMessage( msg );
			state.id = hsm.clientId;
			
			logger.info( "SETTING CLIENT ID TO " + state.id );
			
			state.peers = hsm.peers;
			logger.debug( "Connecting with " + state.peers.length + " peers" );
			
			resp = new HandshakeConfirmationMessage();
			logger.debug( "Sending player " + state.localPlayer );
			resp.player = state.localPlayer.toJSON();
		}
		
		public function get hasResponse() : Boolean
		{
			return hasResp;
		}
		
		public function get response() : AquiferMessage
		{
			return resp;
		}
		
		public function get generatesEvent() : Boolean
		{
			return false;
		}
		
		public function get event() : Event
		{
			return null;
		}
	}
	
}